<template>
	<view style='padding:15px;'>
		<u--form :model="formData" ref="form1">
			<u-form-item label="机构" prop="depCodeParam" @click="showSelect = true;aNum=1;placeName='请输入机构'" labelWidth='150'>
				<u--input v-model="formData.depCodeParam" placeholder="请选择机构" readonly @change='changeInput'>
				</u--input>
			</u-form-item>
			<u-form-item label="检查人员" prop="inspPerId" @click="showSelect = true;aNum=2;placeName='请输入检查人员'" labelWidth='150'>
				<u--input v-model="formData.inspPerId" placeholder="请选择检查人员" readonly>
				</u--input>
			</u-form-item>
			<u-form-item label="检查时间" prop="executionDate" labelWidth='150' @click="showDate = true;">
				<uni-datetime-picker :show="showDate" @close='showDate = false;' v-model="formData.executionDate"
					type="daterange" />
			</u-form-item>
			<u-form-item label="检查对象" prop="inspObjId" @click="showSelect = true;aNum=3;placeName='请输入检查对象'" labelWidth='150'>
				<u--input v-model="formData.inspObjId" placeholder="请选择检查对象" readonly>
				</u--input>
			</u-form-item>
			<u-form-item label="任务节点" prop="taskStatus" @click="showSelect = true;aNum=4;placeName='请输入任务节点'" labelWidth='150'>
				<u--input v-model="formData.taskStatus" placeholder="请选择任务节点" readonly>
				</u--input>
			</u-form-item>
		</u--form>
		<!-- 机构 /企业/人员-->
		<qianziyuSelect :show="showSelect" type="radio"
			:dataLists="aNum==1?columns:aNum==2?columnsName:aNum==3?columnsCompany:taskStatus" :placeholder="placeName"
			@cancel="showSelect=false" @submit="confirmSelect" @search='search'></qianziyuSelect>
		<u-button style='margin:15px auto;width:80%;' type="primary" size="large" @click="btnSearch()">查询</u-button>
		<u-button style='margin:15px auto;width:80%;' type="primary" size="large" @click="btnReset()">重置</u-button>
	</view>
</template>
<script>
	import qianziyuSelect from '@/components/qianziyu-select/qianziyu-select.vue'
	export default {
		components: {
			qianziyuSelect
		},
		data() {
			return {
				formSearch: {
					depCodeParam: "",
					inspObjId: "",
					inspPerId: "",
					taskBeginTime: "",
					taskEndTime: "",
					taskStatus: ""
				},
				formData: {
					depCodeParam: "",
					inspObjId: "",
					inspPerId: "",
					executionDate: "",
					taskStatus: ""
				},
				columns: [],
				columnsCompany: [], //企业名称
				columnsName: [], //人员
				showSelect: false,
				showCompany: false,
				showName: false,
				showDate: false,
				mode: 'range',
				options: {},
				keyList: [], //机构
				companyList: [], //企业
				statusList: [],
				nameList: [],
				aNum: '', //区分是哪个下拉
				taskStatus: [],
				placeName:""

			}
		},
		onLoad(option) {
			this.options = option
		},
		mounted() {
			// 机构
			this.$api.getDeptTreeById(this.$store.state.userInfo.dept_id).then(res => {
				res.data.data.map(item => {
					this.columns.push({
						id: item.value,
						name: item.title
					})
					// 备用
					this.keyList.push({
						id: item.value,
						name: item.title
					})
				})
			})

			// 检查对象
			this.$api.findOwner().then((res) => {
				res.data.data.forEach((item) => {
					this.columnsCompany.push({
						id: item.ownerId,
						name: item.ownerName,
					});
					this.companyList.push({
						id: item.ownerId,
						name: item.ownerName,
					})
				});
			});

			// 任务节点字典
			this.$api.getDictByCode("safety_rectification").then((res) => {
				this.taskStatus = res;
				this.statusList = res
			});
		},
		methods: {
			// 查询
			btnSearch() {
				this.formSearch.taskBeginTime = this.formData.executionDate[0]
				this.formSearch.taskEndTime = this.formData.executionDate[1]
				const searchList = {
					...this.options,
					...this.formSearch
				}
				this.$goBack('./checkTask?direction=2&formSearch=' + encodeURIComponent(JSON.stringify(searchList)),
					5);
			},

			// 机构确定
			confirmSelect(e) {
				if (this.aNum == 1) {
					this.formData.depCodeParam = e.name
					this.formSearch.depCodeParam = e.id
				}
				if (this.aNum == 2) {
					this.formData.inspPerId = e.name
					this.formSearch.inspPerId = e.id
				}
				if (this.aNum == 3) {
					this.formData.inspObjId = e.name
					this.formSearch.inspObjId = e.id
				}
				if (this.aNum == 4) {
					this.formData.taskStatus = e.name
					this.formSearch.taskStatus = e.id
				}
				this.showSelect = false
			},

			// 搜索查询
			search(keyword) {
				const aArr = ['columns', 'columnsName', 'columnsCompany', 'taskStatus']
				let arrList = []
				if (keyword == '') {
					if (this.aNum == 1) {
						this.columns = this.keyList
					} else if (this.aNum == 2) {
						this.columnsName = this.nameList
					} else if (this.aNum == 3) {
						this.columnsCompany = this.companyList
					} else {
						this.taskStatus = this.statusList
					}
				} else {
					this[aArr[this.aNum - 1]].forEach(item => {
						if (item.name.indexOf(keyword) != -1) {
							arrList.push(item)
						}
					})
					this[aArr[this.aNum - 1]] = arrList
				}

			},

			// 修改机构后触发
			changeInput(dept_id) {
				this.formData.inspPerId = ''
				this.formSearch.inspPerId = ''
				let list = []
				this.$api.getDeptUserList(this.formSearch.depCodeParam, this.$store.state.userInfo.tenant_id).then(res => {
					if (res.data.data.length == 0) {
						this.columnsName = []
						this.nameList = []
					} else {
						res.data.data.forEach((item) => {
							this.columnsName.push({
								id: item.id,
								name: item.name,
							});

							this.nameList.push({
								id: item.id,
								name: item.name,
							});
						});
					}


				})
			},

			// 重置
			btnReset() {
				this.$refs.form1.resetFields()
			}


		}
	}
</script>

<style lang="scss" scoped>
</style>
